define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            var fields = {};

            /**
             * 初始化查询
             */
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'crm/adminwork/index/',
                    add_url: 'crm/adminwork/add',
                    edit_url: 'crm/adminwork/edit',
                    del_url: 'crm/adminwork/del',
                    multi_url: 'crm/adminwork/multi',
                    table: 'adminwork',
                },
                queryParams: function (params) {
                    // 自定义搜索条件
                    var filter = params.filter ? JSON.parse(params.filter) : {};
                    var op = params.op ? JSON.parse(params.op) : {};
                    if(ids){
                        filter.id = ids;
                        op.id = '=';
                    }
                    //filter.后跟的是在ajax里使用的名称
                    filter.mid = Config.memid;
                    //op
                    op.mid = 'in';
                    params.filter = JSON.stringify(filter);
                    params.op = JSON.stringify(op);
                    return params;
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {field: 'title', title: __('Title'),operate:'LIKE'},
                        /*{field: 'brief', title: __('Brief')},*/
                        {field: 'workfile', title: __('Workfile'),formatter:Table.api.formatter.url,operate:false},
                        {field: 'speedimages', title: __('Speedimages'), formatter: Table.api.formatter.images,operate:false},
                        {field: 'speedfile', title: __('Speedfile'),formatter:Table.api.formatter.url,operate:false},
                        {field: 'expire_date', title: __('Expire_date'),operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                        {field: 'wan_date', title: __('Wan_date'),operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                        {field: 'qilist', title: __('Qilist'),searchList: {"1":__('Qilist 1'),"2":__('Qilist 2')}, formatter: Table.api.formatter.normal},
                        {field: 'statuslist', title: __('Statuslist'), searchList: {"1":__('Statuslist 1'),"2":__('Statuslist 2'),"3":__('Statuslist 3'),"4":__('Statuslist 4')}, formatter: Table.api.formatter.normal},
                        {field: 'admin_name', title: __('Admin_name'),operate:'LIKE'},
                        {field: 'shen_date', title: __('Shen_date'),operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                        {field: 'shen_name', title: __('Shen_name'),operate:'LIKE'},
                        {field: 'shen_text', title: __('Shen_text'),operate:false},
                        {field: 'admin.nickname', title: __('Admin.nickname'),operate:'LIKE'},
                        {field: 'operate', title: __('Operate'),
                            table: table, events: Table.api.events.operate,
                            buttons:[
                                {
                                    name: '提交',
                                    text: __('提交'),
                                    title: __('提交'),
                                    classname: 'btn btn-xs btn-primary btn-dialog btn-success',
                                    icon: 'fa fa-plus',
                                    url: 'crm/adminwork/sale_add',
                                    callback: function (data) {
                                        Layer.alert("接收到回传数据：" + JSON.stringify(data), {title: "回传数据"});
                                    },
                                    visible: function (row) {
                                        //返回true时按钮显示,返回false隐藏
                                        console.log(row)
                                        if(row['statuslist']!=4&&admin_id==row['admin_id']){
                                            return true;
                                        }else  {
                                            return false
                                        }

                                    }
                                },  {
                                    name: '确认',
                                    text: __('确认'),
                                    title: __('确认'),
                                    extend: 'data-area=\'["350px", "200px"]\'',
                                    classname: 'btn btn-xs btn-primary btn-dialog btn-success',
                                    icon: 'fa fa-check-square-o',
                                    url: 'crm/adminwork/examine/type/4',
                                    callback: function (data) {
                                        Layer.alert("接收到回传数据：" + JSON.stringify(data), {title: "回传数据"});
                                    },
                                    visible: function (row) {
                                        //返回true时按钮显示,返回false隐藏
                                        if(group_id==1||group_id==2){
                                            if(row['statuslist']==2||row['statuslist']==3){
                                                //待验收
                                                return true;
                                            }else{
                                                return false
                                            }
                                        }else {
                                            return false
                                        }
                                    }
                                }, {
                                    name: '驳回',
                                    text: __('驳回'),
                                    title: __('驳回'),
                                    extend: 'data-area=\'["350px", "200px"]\'',
                                    classname: 'btn btn-xs btn-primary btn-dialog btn-success',
                                    icon: 'fa fa-plus',
                                    url: 'crm/adminwork/examine/type/3',
                                    callback: function (data) {
                                        Layer.alert("接收到回传数据：" + JSON.stringify(data), {title: "回传数据"});
                                    },
                                    visible: function (row) {
                                        //返回true时按钮显示,返回false隐藏
                                        console.log(row)
                                        if(group_id==1||group_id==2){
                                            if(row['statuslist']==2){
                                                return true;
                                            }else {
                                                return false;
                                            }
                                        }else {
                                            return false;
                                        }
                                    }
                                }
                            ],
                            formatter:Table.api.formatter.operate
                        }
                    ]
                ]
            });

            /**
             * 初始化
             */
            function ready() {
                if(ids){
                    $('#id').val(ids);
                    ids = '';
                }
            }
            ready()
            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        /**
         * 销售提交任务
         */
        sale_add:function () {
            Controller.api.bindevent();
        },
        /**
         * 审核
         */
        examine:function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});